package com.netcracker.ui;

import com.netcracker.util.service.UserService;
import com.netcracker.util.service.BookService;
import com.netcracker.util.service.PublisherService;
import com.netcracker.util.service.GenreService;
import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.CountryService;
import com.netcracker.util.service.AdminService;
import com.netcracker.util.service.AuthorService;
import com.netcracker.util.dao.impl.PublisherDAOImpl;
import com.netcracker.util.dao.impl.GenreDAOImpl;
import com.netcracker.util.dao.impl.UserDAOImpl;
import com.netcracker.util.dao.impl.BookDAOImpl;
import com.netcracker.util.dao.impl.CountryDAOImpl;
import com.netcracker.util.dao.impl.AuthorDAOImpl;
import com.netcracker.util.dao.impl.LanguageDAOImpl;
import com.netcracker.util.dao.LanguageDAO;
import com.netcracker.util.dao.UserDAO;
import com.netcracker.util.dao.PublisherDAO;
import com.netcracker.util.dao.BookDAO;
import com.netcracker.util.dao.GenreDAO;
import com.netcracker.util.dao.CountryDAO;
import com.netcracker.util.dao.AuthorDAO;
import com.netcracker.util.dao.DAOFacade;
import com.netcracker.db.exception.DBException;
import com.netcracker.db.DBManager;
import com.netcracker.db.impl.OracleDBManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ViewModel {
    private static final Logger LOGGER = LoggerFactory.getLogger(ViewModel.class);
    private final DAOFacade daoFacade;
    private final ServiceFacade serviceFacade;
    private DBManager dBManager;
    
    
    public ViewModel() {
        try {
            dBManager = new OracleDBManager("jdbc:oracle:thin:@localhost:1521:XE","team1","y1234");
        } catch (DBException ex) {
            LOGGER.error(ex.getMessage(), ex);
        } 
        
        daoFacade = new DAOFacade();
        serviceFacade = new ServiceFacade();
        initDAO();
        initService(); 
    }
    
    private void initDAO() {
        BookDAO bookDAO = new BookDAOImpl(dBManager, daoFacade);
        daoFacade.setBookDAO(bookDAO);
        
        AuthorDAO authorDAO = new AuthorDAOImpl(dBManager, daoFacade);
        daoFacade.setAuthorDAO(authorDAO);
        
        CountryDAO countryDAO = new CountryDAOImpl(dBManager, daoFacade);
        daoFacade.setCountryDAO(countryDAO);
        
        GenreDAO genreDAO = new GenreDAOImpl(dBManager, daoFacade);
        daoFacade.setGenreDAO(genreDAO);
        
        LanguageDAO languageDAO = new LanguageDAOImpl(dBManager, daoFacade);
        daoFacade.setLanguageDAO(languageDAO);
        
        PublisherDAO publisherDAO = new PublisherDAOImpl(dBManager, daoFacade);
        daoFacade.setPublisherDAO(publisherDAO);
        
        UserDAO userDAO = new UserDAOImpl(dBManager, daoFacade);
        daoFacade.setUserDAO(userDAO);
    }
    
    private void initService() {
        BookService bookService = new BookService(daoFacade);
        serviceFacade.setBookService(bookService);
        
        AuthorService authorService = new AuthorService(daoFacade);
        serviceFacade.setAuthorService(authorService);
        
        GenreService genreService = new GenreService(daoFacade);
        serviceFacade.setGenreService(genreService);
        
        CountryService countryService = new CountryService(daoFacade);
        serviceFacade.setCountryService(countryService);
        
        PublisherService publisherService = new PublisherService(daoFacade);
        serviceFacade.setPublisherService(publisherService);
        
        UserService userService = new UserService(daoFacade);
        serviceFacade.setUserService(userService);
        
        AdminService adminService = new AdminService(daoFacade);
        serviceFacade.setAdminService(adminService);
    }
    
    public ServiceFacade getServiceFacade() {
        return serviceFacade;
    }
    
    
    public DBManager getDBManager() {
        return dBManager;
    }
}
